﻿//----------------------------------------------------
//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using System.Data.Entity;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 数据访问层CMS_ShoppingCarts。
    /// </summary>
    public class ShoppingCartDal
    {
        #region  是否存在
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="CartGUID"></param>
        /// <returns></returns>
        public bool Exists(string CartGUID)
        {
            using (Entities db = new Entities())
            {
                return db.ShoppingCarts.Any(x => x.CartGUID == CartGUID);
            }
        }
        #endregion

        #region  加入购物车
        /// <summary>
        /// 加入购物车
        /// </summary>
        /// <param name="model"></param>
        public void Add(ShoppingCart model)
        {
            using (Entities db = new Entities())
            {
                if (db.ShoppingCarts.Any(x => x.GoodsId == model.GoodsId && x.CartGUID == model.CartGUID) == false)
                {

                    model.IsBundling = 0;
                    db.ShoppingCarts.Add(model);
                }
                else
                {
                    var q = db.ShoppingCarts.Where(x => x.GoodsId == model.GoodsId && x.CartGUID == model.CartGUID);
                    foreach (var v in q)
                    {
                        v.Quantity = v.Quantity + 1;
                    }
                }

                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Id"></param>
        public void Delete(int Id)
        {
            using (Entities db = new Entities())
            {
                db.ShoppingCarts.Remove(db.ShoppingCarts.Find(Id));

                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="cartGuid"></param>
        /// <param name="userName"></param>
        /// <param name="discount">折扣率</param>
        /// <returns></returns>
        public IList<ShoppingCart> BindList(string cartGuid, string userName, decimal discount)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                var q = from x in db.ShoppingCarts.Include(x=>x.Shop)

                        where x.CartGUID == cartGuid
                        select new { Id = x.Id, GoodsID = x.GoodsId, x.Quantity, PriceMember_Cuted = x.Shop.PriceMember * discount, x.CartGUID, ShopInfo = x.Shop, x.CheckInDate };

                return q.ToList().Select(x => new ShoppingCart()
                {
                    Id = x.Id,
                    GoodsId = x.GoodsID,
                    Quantity = x.Quantity,
                    PriceMember_Cuted = x.PriceMember_Cuted,
                    CartGUID = x.CartGUID,
                    Shop = x.ShopInfo,
                    CheckInDate = x.CheckInDate
                }).ToList();
            }
        }

        #endregion

        #region 显示购物车状态
        /// <summary>
        /// 显示购物车状态
        /// </summary>
        /// <param name="cartGuid"></param>
        /// <returns></returns>
        public DataRow ShowCartState(string cartGuid)
        {
            SqlParameter[] arrParameter = new SqlParameter[1];
            arrParameter[0] = new SqlParameter("CartGUID", SqlDbType.NVarChar, 50) {Value = cartGuid};

            DataRow dr = SQLBaseV2.ReturnDataRow(@"SELECT COUNT(CMS_ShoppingCarts.Quantity) as GoodsQuantity,ISNULL(SUM(CMS_Goods.PriceMember*CMS_ShoppingCarts.Quantity),0)  as PriceTotal
            FROM CMS_Goods INNER JOIN CMS_ShoppingCarts ON  CMS_Goods.Id = CMS_ShoppingCarts.GoodsID
            where CMS_ShoppingCarts.CartGUID=@CartGUID", arrParameter);


            return dr;
        }
        #endregion

        #region 清空
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="CartGUID"></param>
        public void Clear(string CartGUID)
        {
            using (Entities db = new Entities())
            {
                db.Database.ExecuteSqlCommand(string.Format("delete from CMS_ShoppingCarts WHERE CartGUID={0}", CartGUID));
            }
        }
        #endregion

        #region 修改购物车的数量
        /// <summary>
        /// 修改购物车的数量
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="Quantity"></param>
        public void EditShoppingCartQuantity(int Id, int Quantity)
        {
            using (Entities db = new Entities())
            {
                var q = db.ShoppingCarts.Where(x => x.Id == Id).First();
                q.Quantity = Quantity;
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion
    }
}